package com.itqf.service.impl;

import com.itqf.bean.DtsPermission;
import com.itqf.bean.DtsPermissionExample;
import com.itqf.constants.ShiroConstants;
import com.itqf.mapper.DtsPermissionMapper;
import com.itqf.service.DtsPermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/17 10:59
 * description:
 */
@Service
public class DtsPermissionServiceImpl implements DtsPermissionService {

    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;

    private static  final Logger LOGGER = LoggerFactory.getLogger(DtsPermissionService.class);

    /**
     * 根据角色id查询权限！
     * 注意：如果是超级管理员，只需要返回* 即可！
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<String> findPermissionsByRoleIds(Integer[] roleIds) {

        //1.参数判断
        //2.查询permissions
        //role_id in ()  and deleted = 0
        DtsPermissionExample example = new DtsPermissionExample();

        DtsPermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(Arrays.asList(roleIds));
        criteria.andDeletedEqualTo(false);

        List<DtsPermission> permissions = dtsPermissionMapper.selectByExample(example);

        if (permissions == null || permissions.size() == 0) {

            return null;
        }

        //3.赛选超级管理员 *
        List<String> permissionList = new ArrayList<>();
        for (DtsPermission permission : permissions) {

            /**
             * 超级管理员只需要超级权限即可！
             */
            if (permission.getPermission().equalsIgnoreCase(ShiroConstants.SUPER_PERMISSION)) {
                permissionList.clear();
                permissionList.add(ShiroConstants.SUPER_PERMISSION);
                break;
            }

            permissionList.add(permission.getPermission());
        }


        return permissionList;
    }
}
